package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.commen.BaseContext;
import com.itheima.reggie.commen.R;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    //添加到购物车
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart, HttpSession session){
        log.info("shoppingCart{}",shoppingCart);
        Long userid = (Long) session.getAttribute("employee");
        Long dishId = shoppingCart.getDishId();

        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        //先判断添加的是菜品还是套餐
        if (dishId!=null){
            queryWrapper.eq(ShoppingCart::getDishId,dishId);
        }else {
            queryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }

        //判断当前用户添加的菜品,或套餐是否已经添加过了
        queryWrapper.eq(ShoppingCart::getUserId,userid);
        ShoppingCart cartOne = shoppingCartService.getOne(queryWrapper);

        if (cartOne!=null){
            //数量在原有的基础上+1
            cartOne.setNumber(cartOne.getNumber()+1);
            shoppingCartService.updateById(cartOne);
            return R.success(cartOne);
        }else {
            shoppingCart.setUserId(userid);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
            return R.success(shoppingCart);
        }
    }

    @PostMapping("/sub")
    public R<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart, HttpSession session){
        log.info("shoppingCart{}",shoppingCart);
        Long userid = (Long) session.getAttribute("employee");
        Long dishId = shoppingCart.getDishId();
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();

        if (dishId!=null){
            queryWrapper.eq(ShoppingCart::getDishId,dishId);
        }else {
            queryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        queryWrapper.eq(ShoppingCart::getUserId,userid);
        ShoppingCart cartOne = shoppingCartService.getOne(queryWrapper);
        if (cartOne!=null){
            if (cartOne.getNumber()>1){
                //数量在原有的基础上-1
                cartOne.setNumber(cartOne.getNumber()-1);
                shoppingCartService.updateById(cartOne);
            }else{
                shoppingCartService.remove(queryWrapper);
            }
            return R.success(cartOne);
        }
        return R.success(shoppingCart);
    }

    //查询购物车
    @GetMapping("/list")
    public R<List<ShoppingCart>> list(){
        System.out.println("=====+"+BaseContext.getCurrentId());
        //获取当前登录用户的购物车数据
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        //按加入的时间降序
        queryWrapper.orderByAsc(ShoppingCart::getCreateTime);

        List<ShoppingCart> list = shoppingCartService.list(queryWrapper);

        return R.success(list);
    }
}
